import apis from "../services/messageSendApi";

const dep = [{ text: "城市大脑", value: "003800" }];
export default {
  namespace: "messageSend",
  state: {
    messageList: [],
    stateFilter: [{ text: "成功", value: "1" }, { text: "失败", value: "2" }], // 状态列表
    channelDepFilter: dep // 渠道 归属部门列表,
  },
  effects: {
    *fetchMessageList({ payload }, { call, put }) {
      const res = yield call(apis.fetchMessageList, payload);
      const { errCode, datas = {} } = res || {};
      const { list = [], total } = datas || {};
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { messageList: list }
        });
      }
      return total;
    },
    *exportExcel({ payload }, { call }) {
      const res = yield call(apis.exportExcel, payload);
      return res;
    },
    *msgRePush({ payload }, { call }) {
      return yield call(apis.repush, payload);
    },
    *fetchDepList({ payload }, { call, put }) {
      const res = yield call(apis.fetchDepList, payload);
      const { errCode, datas = [] } = res || {};
      const depFilterArr = [];
      for (let i = 0; i < datas.length; i += 1) {
        depFilterArr.push({
          text: datas[i].ownerDeptName,
          value: `${datas[i].ownerDept}`
        });
      }
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { channelDepFilter: depFilterArr }
        });
      }
    }
  },
  reducers: {
    updateState: (state, { payload }) => ({
      ...state,
      ...payload
    })
  }
};
